
import os
from django.core.exceptions import ObjectDoesNotExist

from api.models import Material, MaterialSubType, UploadRecord, RecordTts, RecordVideo
from api.serializers import MaterialSerializer, RecordTtsSerializer, RecordVideoSerializer, MaterialSubTypeSerializer
import littleshinserver.settings

from api.mylogger import logger


def material_exist(material_id):
    try:
        rows = Material.objects.filter(id=material_id)
        if len(rows) == 0:
            return False
        else:
            return True
    except ObjectDoesNotExist:
        return False


def get_material(material_id):
    if material_id is None:
        return {}
    if material_id == 0:
        return {}
    try:
        rows = Material.objects.filter(id=material_id)
        if len(rows) == 0:
            return {}
        else:
            return MaterialSerializer(rows[0]).data
    except ObjectDoesNotExist:
        return {}
    return {}


def get_record_tts(record_id):
    if record_id is None:
        return {}
    if record_id == 0:
        return {}
    try:
        rows = RecordTts.objects.filter(id=record_id)
        if len(rows) == 0:
            return {}
        else:
            return RecordTtsSerializer(rows[0]).data
    except ObjectDoesNotExist:
        return {}
    return {}


def get_record_video(record_id):
    if record_id is None:
        return {}
    if record_id == 0:
        return {}
    try:
        rows = RecordVideo.objects.filter(id=record_id)
        if len(rows) == 0:
            return {}
        else:
            return RecordVideoSerializer(rows[0]).data
    except ObjectDoesNotExist:
        return {}
    return {}


def delete_file(urlpath):
    s_file = urlpath
    if s_file.startswith("/"):
        s_file = urlpath[1:]
    if s_file.startswith("/"):  # 再有以"/"开头的urlpath就奇怪了
        return False
    if not s_file.startswith("static"):  # 必须是static目录
        return False

    # 开始删除文件
    static_base_dir = littleshinserver.settings.STATIC_BASE_DIR
    long_filename = os.path.join(static_base_dir, s_file)
    if os.path.isfile(long_filename):
        try:
            os.remove(long_filename)
            return True
        except:
            return False


def urlpath_in_upload_record():
        try:
            # 一次最多删除1000个
            rows = UploadRecord.objects.filter(status=False)[0:1000]
            list_urlpath = []
            for row in rows:
                list_urlpath.append(row.urlpath)
            return list_urlpath

        except ObjectDoesNotExist:
            return []
        return []


def get_material_subtype_by_id(material_subtype_id):
    if material_subtype_id is None:
        return {}
    if material_subtype_id == 0:
        return {}
    try:
        rows = MaterialSubType.objects.filter(id=material_subtype_id)
        if len(rows) == 0:
            return {}
        else:
            return MaterialSubTypeSerializer(rows[0]).data
    except ObjectDoesNotExist:
        return {}
    return {}


def get_material_subtype(material_type, material_subtype):
    try:
        rows = MaterialSubType.objects.filter(material_type=material_type).filter(material_subtype=material_subtype)
        print('MaterialSubType query', rows.query)
        if len(rows) == 0:
            print('len(rows) == 0 query')
            return []
        return MaterialSubTypeSerializer(rows[0]).data
    except ObjectDoesNotExist:
        return []
    return []


def is_used_material_subtype(material_type, material_subtype):
    try:
        rows = Material.objects.filter(material_type=material_type).filter(material_subtype=material_subtype)
        print(rows.query)
        cnt = rows.count()
        print('cnt is', cnt)
        if cnt == 0:
            return False
        return True
    except ObjectDoesNotExist:
        return True



def get_all_material_subtype():
    try:
        rows = MaterialSubType.objects.all()
        if len(rows) == 0:
            print('len(rows) == 0 query')
            return []
        return MaterialSubTypeSerializer(rows,many=True).data
    except ObjectDoesNotExist:
        return []
    return []


def get_subtypeinfo(subtype_all, material_type,material_subtype):
    for subtypeinfo in subtype_all:
        if subtypeinfo["material_type"] == material_type and subtypeinfo["material_subtype"]==material_subtype:
            return subtypeinfo
    return {}


def material_subtype_name_zh(subtype_all, material_type, material_subtype):

    for subtypeinfo in subtype_all:
        if subtypeinfo["material_type"] == material_type and subtypeinfo["material_subtype"] == material_subtype:
            return subtypeinfo['name_zh']
    return ''


def material_type_name_zh(material_type):
    if material_type == "vtuber":
        return '数字人'
    if material_type == "background":
        return '背景'
    if material_type == "voice":
        return '配音'
    if material_type == "mood":
        return '氛围'
    if material_type == "music":
        return '音乐'
    return ''
